<template>
  <div class="home">
    <el-container>
      <el-aside width="auto">
      <div class="logo"></div>
      <!-- 导航菜单 -->
        <el-menu
          router
          :collapse="isCollapse"
          background-color="#F9F9F9"
          default-active="2"
          class="el-menu-admin"
          @open="handleOpen"
          @close="handleClose">
          <el-submenu :index="item.path"  v-for="item in allRightList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="list.path" v-for="list in item.children" :key="list.id"><i class="el-icon-setting"></i>{{list.authName}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header class="el-header">
          <i class="el-icon-tickets toggle-btn"  @click="toggle"></i>
          <p class="system-title">电商后台管理系统</p>
          <p style="color:white">
            欢迎您！{{userName}}
          <el-button type="warning" round @click="loginOut">退出</el-button>
          </p>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
// @ is an alias to /src
import { getAllRightList } from '@/api'
export default {
  name: 'home',
  components: {
  },
  data () {
    return {
      allRightList: [],
      userName: '',
      isCollapse: false
    }
  },
  created () {
    this.initAllRightList()
  },
  methods: {
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 初始化列表
    initAllRightList () {
      let name = localStorage.getItem('username')
      this.userName = name
      getAllRightList().then(res => {
        // console.log(res)
        this.allRightList = res.data
      })
    },
    // 退出
    loginOut () {
      localStorage.removeItem('mytoken')
      this.$router.push('login')
    }
  }

}
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
